package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import sampleProgr.BasicObject;
import annotationI.SaferParam;

public class MyProxy implements InvocationHandler{

	public BasicObject basic;

	public MyProxy(BasicObject basic){
		this.basic = basic;
	}
	public Object invoke(Object proxy, Method method, Object[] args)
	throws Throwable {

		SaferParam saferAnnotation = method.getAnnotation(SaferParam.class);

		if(saferAnnotation != null){
			int[] which = saferAnnotation.param();
			if(args != null && which != null){
				for(int i = 0; i < args.length; i++){
					if(i >= which.length) break;

					if(args[i] == null && (i + 1) == which[i]){
						System.out.println("parameter" + (i + 1) + " is null");
						return new Integer(0);
					}
				}
			}
		}
		try{
			return method.invoke(basic, args);
		}catch(Exception e){
			System.err.println("exception occur in: " + method.getName() + " -> " + e);
			return new Integer(0);
		}
	}

}
